import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class ImageManager {
    private Map<String, String> imageMap = new HashMap<>();

    // 下载图片并将信息加入集合
    public void downloadImage(String url) {
        try {
            String fileName = url.substring(url.lastIndexOf("/") + 1);
            String savePath = "images/" + fileName;
            FileUtils.saveImage(url, savePath);
            imageMap.put(fileName, savePath);
            FileUtils.appendLog(url, savePath);
            System.out.println("图片下载成功: " + fileName);
        } catch (IOException e) {
            System.out.println("图片下载失败: " + e.getMessage());
        }
    }

    // 遍历集合，打印已下载图片列表
    public void listImages() {
        if (imageMap.isEmpty()) {
            System.out.println("没有已下载的图片");
            return;
        }

        System.out.println("已下载的图片列表:");
        Set<String> fileNames = imageMap.keySet();
        for (String fileName : fileNames) {
            System.out.println("------ " + fileName + " (" + imageMap.get(fileName) + ")");
        }
    }

    // 根据名称查找图片路径
    public void searchImage(String name) {
        boolean found = false;

        for (String fileName : imageMap.keySet()) {
            if (fileName.contains(name)) {
                System.out.println("找到图片: " + fileName + "，路径: " + imageMap.get(fileName));
                found = true;
            }
        }

        if (!found) {
            System.out.println("没有找到包含 \"" + name + "\" 的图片");
        }
    }

}
